package com.coincalf.task.event;

import com.coincalf.chan.feign.MessagingServiceClient;
import com.coincalf.dto.TradeAreaDTO;
import com.coincalf.dto.TradeMarketDTO;
import com.coincalf.feign.MarketServiceClient;
import com.coincalf.feign.TradingAreaServiceClient;
import com.coincalf.framework.constants.Constant;
import com.coincalf.framework.dto.ForexMarketDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiang
 */
@Component
@Slf4j
public class MarketEvent implements IEvent {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private MarketServiceClient marketServiceClient;
    @Autowired
    private TradingAreaServiceClient tradingAreaServiceClient;
    @Autowired
    private MessagingServiceClient messagingServiceClient;

    @Override
    public void handle() {
        // 推送币币交易对信息
        dc();
        // 推送创新交易对信息
        tc();
    }

    public void dc() {
        List<TradeAreaDTO> tradeAreaList = tradingAreaServiceClient.tradingAreaList();
        tradeAreaList.forEach(tradeArea -> {
            String groupName = String.format("market.%s.ticker", tradeArea.getCode()).toLowerCase();
            List<TradeMarketDTO> markets = marketServiceClient.queryTradeMarketsByIds(tradeArea.getMarketIds());
            if (CollectionUtils.isNotEmpty(markets)) {
                try {
                    Map<String, List<TradeMarketDTO>> body = new HashMap<>();
                    body.put("markets", markets);
                    messagingServiceClient.convertAndSendToGroup(groupName, objectMapper.writeValueAsString(body));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        });
        List<TradeMarketDTO> markets = marketServiceClient.queryTradeMarkets();
        if (CollectionUtils.isNotEmpty(markets)) {
            try {
                Map<String, List<TradeMarketDTO>> body = new HashMap<>();
                body.put("markets", markets);
                messagingServiceClient.convertAndSendToGroup(Constant.CH_MARKETS_TICKER, objectMapper.writeValueAsString(body));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        marketServiceClient.tradeMarkets().forEach(t -> {
            try {
                String groupName = String.format("market.%s.detail", t.getSymbol()).toLowerCase();
                List<TradeMarketDTO> marketList = marketServiceClient.queryTradeMarketsByIds(t.getId().toString());
                Map<String, List<TradeMarketDTO>> body = new HashMap<>();
                body.put("tick", marketList);
                messagingServiceClient.convertAndSendToGroup(groupName, objectMapper.writeValueAsString(body));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        });
    }

    public void tc() {
        List<TradeAreaDTO> forexAreaList = tradingAreaServiceClient.forexAreaList();
        forexAreaList.forEach(tradeArea -> {
            String groupName = String.format("market.%s.ticker.tc", tradeArea.getCode()).toLowerCase();
            List<ForexMarketDTO> markets = marketServiceClient.queryForexMarketsByIds(tradeArea.getMarketIds());
            if (CollectionUtils.isNotEmpty(markets)) {
                try {
                    Map<String, List<ForexMarketDTO>> body = new HashMap<>();
                    body.put("markets", markets);
                    messagingServiceClient.convertAndSendToGroup(groupName, objectMapper.writeValueAsString(body));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        });
        List<ForexMarketDTO> markets = marketServiceClient.queryForexMarkets();
        if (CollectionUtils.isNotEmpty(markets)) {
            try {
                Map<String, List<ForexMarketDTO>> body = new HashMap<>();
                body.put("markets", markets);
                messagingServiceClient.convertAndSendToGroup("market.ticker.tc", objectMapper.writeValueAsString(body));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

}
